//
//  XMGHeadView.m
//  weibo
//
//  Created by DP on 16/7/3.
//  Copyright © 2016年 DP. All rights reserved.
//

#define ScreenW [UIScreen mainScreen].bounds.size.width
#define ScreenH [UIScreen mainScreen].bounds.size.height
#import "XMGHeadView.h"
#import <AFNetworking.h>
#import "YYHttpTool.h"

@interface XMGHeadView () 
@property (weak, nonatomic) IBOutlet UILabel *day;
@property (weak, nonatomic) IBOutlet UILabel *yearAndMonth;
@property (weak, nonatomic) IBOutlet UILabel *weather;
@property (weak, nonatomic) IBOutlet UILabel *week;
@property (weak, nonatomic) IBOutlet UIImageView *adPicture;

@end
@implementation XMGHeadView

- (void)awakeFromNib {
    [super awakeFromNib];
    self.frame = CGRectMake(0, 20, ScreenW, ScreenH -20);
    self.backgroundColor = [UIColor whiteColor];
    //设置年月日，星期
    [self setUp];
    //发送网络请求，获取天气
    [self setUpWeather];
   }
//设置年月日，星期
- (void)setUp {
    NSDate *currentDate = [NSDate date];
    NSCalendar *canlender = [NSCalendar currentCalendar];
    NSCalendarUnit type =  NSCalendarUnitYear| NSCalendarUnitMonth| NSCalendarUnitDay| NSCalendarUnitWeekday;
    NSDateComponents *compt = [canlender components:type fromDate:currentDate];
    NSString *yearAndMonth = [NSString stringWithFormat:@"0%ld/%ld",compt.month,compt.year];
    NSString *day = [NSString stringWithFormat:@"0%ld",compt.day];
    NSString *week = [NSString stringWithFormat:@"星期%ld",compt.weekday-1];
    
    self.day.text = day;
    self.week.text = week;
    self.yearAndMonth.text = yearAndMonth;
}
//设置天气
- (void)setUpWeather {
    
    [YYHttpTool get:@"http://www.weather.com.cn/data/cityinfo/101280101.html" params:nil success:^(id responseObj) {
                NSDictionary *dict =  [NSJSONSerialization JSONObjectWithData:responseObj options:NSJSONReadingAllowFragments error:nil];
        NSDictionary *weatherDict = dict[@"weatherinfo"];
        NSString *weatherStr = [NSString stringWithFormat:@"%@:%@%@",weatherDict[@"city"],weatherDict[@"weather"],weatherDict[@"temp2"]];
        self.weather.text = weatherStr;
        
        
    } failure:nil];

}

@end
